package com.ssm.demo.controller;

import com.ssm.demo.pojo.VO.ResultVO;
import com.ssm.demo.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public ResultVO<?> upload(MultipartFile file, HttpServletRequest request)throws IOException {
        //生成新的文件名
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString().replace("-","")+ext;

        //获取Nginx的files目录
        String dir = "C:\\Users\\48318\\Desktop\\Files\\nginx-1.25.3\\html\\files";
        file.transferTo( new File(dir+"/"+fileName));
        return R.successR("http://127.0.0.1/files/"+fileName);

        //获取当前项目中的files目录
        //( 不是files在工作空间的路径，而是项目部署到tomcat之后的files目录路径)
        //String dir2 = request.getSession().getServletContext().getRealPath("files");
        //file.transferTo( new File(dir2+"/"+fileName));
        //return new ResultVO<>(0,"上传成功","/files/"+fileName);
    }


}
